#include "component/Animation.h"
#include "util/FPS.h"
#include <iostream>

using namespace eats_core;

Animation::Animation(SDL_Surface * _working_surf, std::string atlasPath, int * _posX, int * _posY, bool relative, int _maxFrames, int _tileSize){

	posX = _posX;
	posY = _posY;
	tileSize = _tileSize;
	atlas = new Doodle(atlasPath, _working_surf, posX, posY, relative);

	oldTime = SDL_GetTicks();
	oscillate = false;
	maxFrames = _maxFrames;

	frameInc = 1;
	currentFrame = 0;

	frameRate = 100;

}

Animation::~Animation()
{
    //dtor
}

bool Animation::render()
{
	return atlas->render(0, currentFrame*tileSize, tileSize, tileSize);
}

bool Animation::update()
{
	if (oldTime +  frameRate > SDL_GetTicks()) {
		return true;
	}

	oldTime = SDL_GetTicks();

	currentFrame += frameInc;

	if (oscillate) {
		if (frameInc > 0) {
			if (currentFrame >= maxFrames) {
				frameInc = -frameInc;
			}
		} else {
			if (currentFrame <= 0) {
				frameInc = -frameInc;
			}
		}
	} else {
		if (currentFrame >= maxFrames) {
			currentFrame = 0;
		}
	}

	return true;
}


bool Animation::setUp()
{
	std::cout << "|INFO| Carregando o atlas para animacao " << std::endl;

	if(atlas->setUp() == false ) {
		std::cout << "|ERROR| Não foi possível carregar o atlas para animacao " << std::endl;
		return false;
	}

	std::cout << "|INFO| O atlas foi carregado com sucesso" <<  std::endl;

	return true;
}





